package com.animations;

import com.beans.BeginBean;
import com.beans.BirdBean;
import creator.ui.controller.VoicePlayer;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.bean.Switch;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BirdFlyAction extends Animation {

  private static boolean isDown = false;
  public static boolean isArrive = false;
  public static final int floorY =
    BeginBean.getBeginY() + BeginBean.getBeginH() - BirdBean.getBirdH() + 6;

  public static boolean hiddenBird = false;

  public static boolean justOnce = true;

  public void loadBean() {}

  public void paint(Graphics g) {}

  public void run() {
    LoadMouseListener(getMagicanvas());
    new Thread(
      new Runnable() {
        public synchronized void run() {
          while (Switch._isContinue()) {
            BirdFlyAction.doFinallyFallUnder(
              BirdFlyAction.this.getMagicanvas()
            );
            try {
              Thread.sleep(300L);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
      }
    )
      .start();
  }

  private void LoadMouseListener(Magicanvas magicanvas) {
    magicanvas.addMouseListener(
      new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          BirdFlyAction.isDown = true;
          BirdFlyAction.isArrive = true;
          BirdFlyAction.doFinallyFallUnder(BirdFlyAction.this.getMagicanvas());
        }

        public void mouseReleased(MouseEvent e) {
          BirdFlyAction.isDown = false;
          BirdFlyAction.isArrive = false;
          if (
            (Tag._isPlay()) && (Tag._isDisappear()) && (BirdBean._isAlive())
          ) {
            VoicePlayer.play("/res/sounds/sfx_wing.wav");
            BirdFlyAction.access$1(BirdFlyAction.this.getMagicanvas());
          }
          BirdFlyAction.doFinallyFallUnder(BirdFlyAction.this.getMagicanvas());
        }
      }
    );
    magicanvas.addKeyListener(
      new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
          BirdFlyAction.isDown = true;
          BirdFlyAction.isArrive = true;
          BirdFlyAction.doFinallyFallUnder(BirdFlyAction.this.getMagicanvas());
        }

        public void keyReleased(KeyEvent e) {
          BirdFlyAction.isDown = false;
          BirdFlyAction.isArrive = false;
          if (
            (Tag._isPlay()) && (Tag._isDisappear()) && (BirdBean._isAlive())
          ) {
            BirdFlyAction.access$1(BirdFlyAction.this.getMagicanvas());
          }
          BirdFlyAction.doFinallyFallUnder(BirdFlyAction.this.getMagicanvas());
        }
      }
    );
  }

  private static void flyAction(Magicanvas mgicav) {
    new Thread(
      new Runnable(mgicav) {
        public synchronized void run() {
          BirdFlyAction.hiddenBird = false;
          BirdFlyAction.access$2(BirdFlyAction.this);
          BirdFlyAction.access$3(BirdFlyAction.this);
          BirdFlyAction.doFallUnder(BirdFlyAction.this);
        }
      }
    )
      .start();
  }

  private static void doFly(Magicanvas mgicav) {
    int nowY = BirdBean.getBirdY();
    while (
      (BirdBean.getBirdY() > nowY - 52) && (!isArrive) && (BirdBean._isAlive())
    ) {
      BirdBean.setBirdY(BirdBean.getBirdY() - 1);
      if (nowY < 0) {
        BirdBean.setBirdY(0);
        break;
      }
      try {
        Thread.sleep(4L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      mgicav.repaint();
    }
  }

  private static void doBitFallUnder(Magicanvas mgicav) {
    int nowY = BirdBean.getBirdY();
    while (
      (BirdBean.getBirdY() < nowY + 4) && (!isArrive) && (BirdBean._isAlive())
    ) {
      BirdBean.setBirdY(BirdBean.getBirdY() + 1);
      try {
        Thread.sleep(6L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      mgicav.repaint();
    }
    isArrive = true;
  }

  public static void doFallUnder(Magicanvas mgicav) {
    int nowY = BirdBean.getBirdY();
    while ((!isDown) && (isArrive) && (BirdBean._isAlive())) {
      nowY++;
      BirdBean.setBirdY(nowY);
      if (nowY > floorY - 1) {
        VoicePlayer.play("/res/sounds/sfx_hit.wav");
        BirdBean._setAlive(false);
        break;
      }
      mgicav.repaint();
      try {
        Thread.sleep(4L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public static void doFinallyFallUnder(Magicanvas mgicav) {
    new Thread(
      new Runnable(mgicav) {
        public synchronized void run() {
          try {
            int nowY = BirdBean.getBirdY();
            if (!BirdBean._isAlive()) {
              if (
                (nowY < BirdFlyAction.floorY - BeginBean.getBeginH()) &&
                (BirdFlyAction.justOnce)
              ) {
                VoicePlayer.play("/res/sounds/sfx_die.wav");
                BirdFlyAction.justOnce = false;
              }
              while (
                (nowY < BirdFlyAction.floorY - BeginBean.getBeginH()) &&
                (Switch._isContinue())
              ) {
                nowY++;
                BirdBean.setBirdY(nowY);
                BirdFlyAction.this.repaint();
                Thread.sleep(3L);
              }
              BirdFlyAction.hiddenBird = true;
              BirdFlyAction.this.repaint();
            }
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    )
      .start();
  }
}
